<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
	public $name = 'Users';
	public $uses = array('User');

	public function index() {
		
	}
	
	public function add() {
		$this->layout = 'ajax';
	}
	
	public function edit() {
		$this->layout = 'ajax';
	}
	
	public function save() {
		$success = false;
		if($success){
			$this->Session->write('status', 'success');
			$this->Session->setFlash("<strong>Well done!</strong> You successfully save data.");
		}else{
			$this->Session->write('status', 'error');
			$this->Session->setFlash("<strong>Oh snap!</strong> Change a few things up and try submitting again.");
		}
		$this->redirect($this->referer());
	}
	
	public function deleteConfirm() {
		$this->layout = 'ajax';
	}
	
	public function delete() {
		$success = true;
		if($success){
			$this->Session->write('status', 'success');
			$this->Session->setFlash("<strong>Well done!</strong> You successfully delete data.");
		}else{
			$this->Session->write('status', 'error');
			$this->Session->setFlash("<strong>Oh snap!</strong> Can't delete data, please try again.");
		}
		$this->redirect($this->referer());
	}
	/*
	public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->redirect($this->Auth->redirect());
			} else {
				$this->Session->write('status', 'error');
				$this->Session->setFlash(__('Invalid username or password, try again'));
			}
		}
	}

	public function logout() {
		$this->redirect($this->Auth->logout());
	}
	
	//For Google Indentity Toolkit
	
	function userStatus(){
		$this->layout = 'ajax';
		$email = $_POST['email'];
		//$email = 'its@sbm-itb.ac.id';
		$is_exist = $this->User->field('User.id',array('User.email'=>$email));
		if($is_exist){
			$registered = array('registered'=>true);
			//echo "{ 'registered': true }";
		}else{
			//echo "{ 'registered': false }";
			$registered = array('registered'=>false);
		}
		echo json_encode($registered);
		exit;
	}
	
	function userLogin(){
		$this->layout = 'ajax';
		$target = isset($_POST['rp_target']) ? $_POST['rp_target'] : '';
		$email = isset($_POST['email']) ? $_POST['email'] : '';
		$password = isset($_POST['password']) ? $_POST['password'] : '';
		$this->contextLoader();
		App::import('Vendor', 'gitLoginHandler', array('file' =>'handler'.DS.'gitLoginHandler.php'));
		$handler = new gitLoginHandler($target, $email, $password);
		$handler->execute();		
		//exit;
	}
	
	function callback(){
		Configure::write('debug', 2);
		$inputEmail = isset($_GET['rp_input_email']) ? $_GET['rp_input_email'] : '';
		$purpose = isset($_GET['rp_purpose']) ? $_GET['rp_purpose'] : '';
		//die($purpose);
		App::import('Vendor', 'gitUtil', array('file' =>'git'.DS.'util'.DS.'gitUtil.php'));
		$gitUtil = new gitUtil();
		
		$url = $gitUtil->getCurrentUrl();
		$url = str_replace('&amp;', '&', $url);
		$idpResponse = @file_get_contents('php://input');
		
		//ContextLoader::load($this->registry);
		$this->contextLoader();
		App::import('Vendor', 'gitCallbackHandler', array('file' =>'git'.DS.'handler'.DS.'gitCallbackHandler.php'));
		$handler = new gitCallbackHandler($inputEmail, $purpose, $url, $idpResponse);
		$handler->execute();
		exit;
	}
	
	public static function contextLoader(){
		App::import('Vendor', 'gitConfig', array('file' =>'git'.DS.'util'.DS.'gitConfig.php'));
		$config = new gitConfig();
		$config->setApiKey('AIzaSyBpYpzAKWnPp9yLwu4nD-_NIofyXRTIw70');
		$config->setHomeUrl('http://thesis.primagain.com/logs');
		$config->setSignupUrl('http://thesis.primagain.com/users/userSignup');
		$config->sessionUserKey = 'user_id';
		$config->idpAssertionKey = 'idpAssertion';
		App::import('Vendor', 'gitContext', array('file' =>'git'.DS.'util'.DS.'gitContext.php'));
		$gitContext = new gitContext();
		$gitContext->setConfig($config);
			
		App::import('Vendor', 'AccountService', array('file' =>'AccountService.php'));
		gitContext::setAccountService(new AccountService());
		App::import('Vendor', 'SessionManager', array('file' =>'SessionManager.php'));
		gitContext::setSessionManager(new SessionManager());

	}
	
	function userSignup(){
		//$this->layout = 'ajax';
		//echo 'register';
		$this->redirect(array('controllers'=>'logs','action'=>'index'));
	}
	
	function userSignout(){
		$this->Session->destroy();
        $this->redirect($this->Auth->logout());
	}
	
	function logoutAll(){
		//Configure::write('debug', 1);
		$user = $_SESSION['Auth'];
		$email = $user['User']['email'];
		$this->Session->destroy();		
		if(!strstr($email, 'yahoo')){
			header('Location: https://www.google.com/accounts/Logout');
		}else{
			header('https://login.yahoo.com/config/login?logout=1');
		}
	}*/
}
